package basic.socket;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author bingo
 * @Description
 * @Date 2018/7/18
 */
public class ServerDemo1 {

    public static void main(String[] args) {
        try {
            ServerSocket  serverSocket = new ServerSocket(8888);
            Socket socket = serverSocket.accept();
            System.out.println("服务端收到的Socket:"+socket.hashCode());
            System.out.println("1```````````````");
            InputStream inputStream = socket.getInputStream();
            System.out.println("2```````````````");
            InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
            BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

            String res = null;
            String info = "";

            while ((res = bufferedReader.readLine())!= null){
                info+=res;
                System.out.println("```````````````");
                System.out.println("服务端受到客户但消息"+info+","+socket.getInetAddress().getHostAddress());

            }

                        OutputStream outputStream=socket.getOutputStream();//获取一个输出流，向服务端发送信息
                        PrintWriter printWriter=new PrintWriter(outputStream);//将输出流包装成打印流
                        printWriter.print("你好，服务端已接收到您的信息");
                        outputStream.write(8);
                        printWriter.flush();
                        //basic.socket.shutdownOutput();//关闭输出流
                        printWriter.close();
                        outputStream.close();
                        bufferedReader.close();
                        inputStream.close();
                        socket.close();



        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}
